var express = require('express')
var config = require('./config/index')
const axios = require('axios')

const app = express()

var apiRoutes = express.Router()

apiRoutes.get('/getDiscList', function (req, res) {
  var url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
  //发送http请求修改referer和host,欺骗Q音我们的herders和它一样
  axios.get(url, {
    headers: {
      // referer: 'https://y.qq.com/portal/playlist.html'
      referer: 'https://y.qq.com/',
      host: 'c.y.qq.com'
    },
    params: req.query
  }).then((response) => {
    // 数据给我们的浏览器前端
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
  })
})
apiRoutes.get('/music', function (req, res) {//这里的路径是给前端发送请求的url
  const url = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg'
  // axios发送get请求，可以自己配置config
  axios.get(url, {
    headers: {
      referer: 'https://c.y.qq.com/',
      host: 'c.y.qq.com'
    },
    //  params是即将与请求一起发送的url参数，无格式对象/URLSearchParams对象
    params: req.query
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
  })
}),
  apiRoutes.get('/lyric', function(req, res){
    var url="https://szc.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg"

    axios.get(url, {
      headers: { //通过node请求QQ接口，发送http请求时，修改referer和host
        referer: 'https://y.qq.com/',
        host: 'c.y.qq.com'
      },
      params: req.query //把前端传过来的params，全部给QQ的url
    }).then((response) => {
      var ret = response.data
      if (typeof ret === 'string') {
        var reg = /^\w+\(({[^()]+})\)$/
        // 以单词a-z，A-Z开头，一个或多个
        // \(\)转义括号以（）开头结尾
        // （）是用来分组
        // 【^()】不以左括号/右括号的字符+多个
        // {}大括号也要匹配到
        var matches = ret.match(reg)
        if (matches) {
          ret = JSON.parse(matches[1])
          // 对匹配到的分组的内容进行转换
        }
      }
      res.json(ret)
    }).catch((e) => {
      console.log(e)
    })
  }),
  apiRoutes.get('/getSongList', function (req, res) {
    var url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'
    axios.get(url, {
      headers: {
        referer: 'https://y.qq.com/',
        host: 'c.y.qq.com'
      },
      params: req.query
    }).then((response) => {
      res.json(response.data)
    }).catch((e) => {
      console.log(e)
    })
  }),
  apiRoutes.get('/search', (req, res) => {
    var url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp'
    axios.get(url, {
      headers: {
        referer: 'https://y.qq.com/m/index.html',
        host: 'c.y.qq.com'
      },
      params: req.query // 通过req从浏览器端发过来的一堆参数(platform，sin，ein等)透传给qq的服务端
    }).then((response)=>{ // qq服务端的响应数据，再通过res将响应数据输出到浏览器端
      res.json(response.data)
    }).catch((error)=>{
      console.log(error)
    })
  })


app.use('/api',apiRoutes)

app.use(express.static('./dist'))



var port = process.env.PORT || config.build.port

module.exports = app.listen(port, function (err) {
  if(err){
    console.log(err)
  return
  }
  console.log('Listening at http://localhost:' + port + '\n')
})


